﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PlaydeezGames.Common;

namespace IslandInterloper.Engine
{
    internal class Commodity : GameClient
    {

        private string name;
        public string Name
        {
            get
            {
                return name;
            }
        }

        private double basePrice;
        public double BasePrice
        {
            get
            {
                return basePrice;
            }
        }

        private double demandFactor;
        public double DemandFactor
        {
            get
            {
                return demandFactor;
            }
        }

        private double supplyFactor;
        public double SupplyFactor
        {
            get
            {
                return supplyFactor;
            }
        }

        private double tonsPerUnit;
        public double TonsPerUnit
        {
            get
            {
                return tonsPerUnit;
            }
        }

        private bool traded;
        public bool Traded
        {
            get
            {
                return traded;
            }
        }
        public bool hidden;
        public bool Hidden
        {
            get
            {
                return hidden;
            }
        }
        public double Markup
        {
            get;
            private set;
        }

        private WeightedGenerator<int> supplyGenerator;
        public int GenerateSupply()
        {
            return supplyGenerator.Generate();
        }

        private WeightedGenerator<int> demandGenerator;
        public int GenerateDemand()
        {
            return demandGenerator.Generate();
        }

        private WeightedGenerator<int> inventoryGenerator;
        public int GenerateInventory()
        {
            return inventoryGenerator.Generate();
        }

        internal Commodity(Game theGame, XElement element)
            : base(theGame)
        {
            FromXElement(element);
        }
        private void FromXElement(XElement element)
        {
            name = element.Element(GameXNames.name).Value;
            basePrice = Utilities.XElementToDouble(element.Element(GameXNames.basePrice));
            Markup = Utilities.XElementToDouble(element.Element(GameXNames.markup));
            demandFactor = Utilities.XElementToDouble(element.Element(GameXNames.demandFactor));
            supplyFactor = Utilities.XElementToDouble(element.Element(GameXNames.supplyFactor));
            tonsPerUnit = Utilities.XElementToDouble(element.Element(GameXNames.tonsPerUnit));
            traded = (element.Element(GameXNames.traded) != null);
            hidden = (element.Element(GameXNames.hidden) != null);
            supplyGenerator = new WeightedGenerator<int>(element.Element(GameXNames.supplyGenerator), Utilities.FromStringToInt);
            demandGenerator = new WeightedGenerator<int>(element.Element(GameXNames.demandGenerator), Utilities.FromStringToInt);
            inventoryGenerator = new WeightedGenerator<int>(element.Element(GameXNames.inventoryGenerator), Utilities.FromStringToInt);
        }
        public override XElement ToXElement(XName xName)
        {
            XElement result = new XElement(xName,
                new XElement(GameXNames.name, name),
                new XElement(GameXNames.basePrice, basePrice.ToString()),
                new XElement(GameXNames.markup,Markup.ToString()),
                new XElement(GameXNames.demandFactor, demandFactor.ToString()),
                new XElement(GameXNames.supplyFactor, supplyFactor.ToString()),
                new XElement(GameXNames.tonsPerUnit, tonsPerUnit.ToString()),
                supplyGenerator.ToXElement(GameXNames.supplyGenerator),
                demandGenerator.ToXElement(GameXNames.demandGenerator),
                inventoryGenerator.ToXElement(GameXNames.inventoryGenerator)
                );
            if (traded)
            {
                result.Add(new XElement(GameXNames.traded));
            }
            if (hidden)
            {
                result.Add(new XElement(GameXNames.hidden));
            }
            return result;
        }
    }
}
